package com.bdcom.autotest.telnet.invoke.impl;

import com.bdcom.autotest.telnet.TelnetClientForCOM;
import com.bdcom.autotest.telnet.TelnetCommunication;
import com.bdcom.autotest.telnet.constant.TelnetConstant;
import com.bdcom.autotest.telnet.invoke.DeveiceOper;
import com.bdcom.autotest.telnet.model.DevIPObj;
import com.bdcom.autotest.telnet.model.Result;
import com.bdcom.autotest.telnet.model.TelnetCMDObj;
import com.bdcom.autotest.telnet.util.TelnetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 设备重启，分为monitor下/master重启，重启成功又分为进入monitor或master模式
 * Result
 */
public class DevReboot implements DeveiceOper, TelnetConstant {
    private static final Logger logger = LoggerFactory.getLogger(DevReboot.class);
    @Override
    public Result executeCMD(TelnetCMDObj obj) {

        Result result=new Result();
        TelnetClientForCOM clientForCOM=null;
        try {
            TelnetCommunication communication=new TelnetCommunication();
            clientForCOM=new TelnetClientForCOM(obj.getIp(),obj.getPort());
            Result loginResult=communication.login(obj.getUser(),obj.getPassword(),clientForCOM,obj.isMonitor());
            if(loginResult.getStatus()){
                Result enableResult = communication.enableOper(clientForCOM, obj.getEnablePassword(),obj.isMonitor());
                if(enableResult.getStatus()){
                    String rebootRet = clientForCOM.sendCommand("reboot", 1000L);
                    if(rebootRet.contains("y/n")){
                        rebootRet=communication.sendReboot(clientForCOM);
                        result = TelnetUtil.executeStatusForReboot(rebootRet);

                    }else{
                        result.setStatus(false);
                        result.setFailedReason("execute reboot,can't obtain select y/n");
                    }

                    return result;

                }else{
                    return enableResult;
                }
            }else{
                return loginResult;
            }
            

        }catch (Exception e){
            result.setFailedReason(e.toString());
            return result;
        }finally {
            if(null!=clientForCOM){
                try {

                    if(!obj.isMonitor()){
                        //非monitor模式下恢复telnet连接的COM口的初始状态
                        //clientForCOM.sendCommand("exit",200L);
                        //clientForCOM.sendCommand("exit",200L);
                    }

                    clientForCOM.disConnect();
                } catch (Exception e) {
                    logger.error(e.toString());
                }
            }
        }


    }

    /**
     * 设备重启，分为monitor下/master重启，重启成功又分为进入monitor或master模式
     *  *
     * @param obj       TelnetCMDObj中的isMonitor属性控制monitor还是master下重启
     * @return Result   Result中的getRetMsg可以获取重启后进入monitor还是master模式
     * getRetMsg 枚举值  Master/Monitor
     */
    public Result reboot(TelnetCMDObj obj){
        Result result = executeCMD(obj);
        return result;


    }
}
